package com.tonglin.springboot.controller;

/**
 *
 * 接受数据,把数据传到主页
 *
 */

import com.tonglin.springboot.po.Book;
import com.tonglin.springboot.po.Status;
import com.tonglin.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BookController {

    String userName;

    public BookController() {
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Autowired
    BookService bookService;

    BookAdressController bookAdressController = new BookAdressController();

    @RequestMapping("showBooks")
    public String showBoos(Model model) {

        List<Book> bookList = bookService.showBooks();

       if(bookList!=null){
           model.addAttribute("books",bookList);
       }
            return "booksHomePage";
    }

    @RequestMapping("back")
    public  String order(Model model){

        List<Book> bookList = bookService.showBooks();
        if(bookList!=null){
            model.addAttribute("books",bookList);
        }
        return "borrow";
    }

    @RequestMapping("order")
    public String reciveOrder(String userName,String bookName,Model model){

      String msg;

        System.out.println(this.userName);

       if(!userName.equals(this.userName)){


           msg = "借书的用户名与登录的用户名不一致！";
           model.addAttribute("msg",msg);
           return "borrowMsg";
       }else {

            msg = bookService.updateOrder(userName,bookName);
           model.addAttribute("msg",msg);

           return "borrowMsg";
       }




    }

    //将搜索信息返回到页面
    @RequestMapping("search")
    public String searchBook(String bookName,Model model){


        Status status = bookService.returnSearchBook(bookName);

        if(status.isCode()){
            return "redirect:https://baike.baidu.com";
        }else {
            model.addAttribute("msg",status.getMsg());

            return "searchBooksMsg";
        }
    }
    //还书
    @RequestMapping("returnBook")
    public  String returnBook(Model model){

        List<Book> bookList = bookService.showBooks();
        if(bookList!=null){
            model.addAttribute("books",bookList);
        }
        return "return";
    }
    @RequestMapping("doReturn")
    public String doReturn (String userName,String bookName,Model model){

//        String msg;
//
//        System.out.println(this.userName);
//
//        if(!userName.equals(this.userName)){
//
//
//            msg = "借书的用户名与登录的用户名不一致！";
//            model.addAttribute("msg",msg);
//            return "borrowMsg";
//        }else {
//
//            msg = bookService.updateOrder(userName,bookName);
//            model.addAttribute("msg",msg);
//
//            return "borrowMsg";
//        }

        String msg;

        if(!userName.equals(this.userName)){
            msg = "还书的用户名与登录的用户名不一致！";
            model.addAttribute("msg",msg);
            return "returnMsg";
        }else {

            msg = bookService.doReturn(userName,bookName);
            model.addAttribute("msg",msg);


            return "returnMsg";
        }

    }
    @RequestMapping("leaveMsg")
    public String leaveMsg(String msg,Model model){




        return "leaveMsgHtml";
    }

    @RequestMapping("insertMsg")
    public String inserMsg(String msg,Model model){

        String msg01 =  bookService.leaveMsg(msg,this.userName);

        model.addAttribute("msg",msg01);

        return "msgMsg";
    }


    }




